<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <platform-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新增
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </template>

      

        <template v-slot:sort>
          <i
            class="sort-handle el-icon-rank ele-text-secondary"
            style="cursor: move"
          ></i>
        </template>

            <template v-slot:status="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>


    <template v-slot:application_rate ="{ row }">
         <div class="ele-cell-title">{{ row.application_rate }} %</div>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>

          <el-link type="primary" :underline="false" @click="resetPsw(row)">
            重置密码
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <platform-edit :visible.sync="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import PlatformSearch from './components/platform-search';
import PlatformEdit from './components/platform-edit';
import { list, remove, modify, doDefault, sortChange ,resetPassword} from '@/api/platform';
import Sortable from 'sortablejs';

export default {
  name: 'Platform',
  components: {
    PlatformSearch,
    PlatformEdit
  },
  data() {
    return {
      host: window.location.protocol + '//' + location.host,
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },

        {
          prop: 'name',
          label: '平台名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'telphone',
          label: '平台手机号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'url',
          label: '平台域名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'application_rate',
          label: '进件手续费',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'application_rate',
        },
            {
          prop: 'status',
          label: '是否启用',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'status'
        },

        {
          prop: 'create_time',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 150,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },

        {
          prop: 'expiration_time',
          label: '到期时间',
          showOverflowTooltip: true,
          minWidth: 130
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 350,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示分配回收员
      showCollector: false,
      //弹框名称
      id: 0
    };
  },
  methods: {
    copy_url(content) {
      if (window.clipboardData) {
        window.clipboardData.setData('text', content);
      } else {
        (function (content) {
          //oncopy 事件在用户拷贝元素上的内容时触发。
          document.oncopy = function (e) {
            e.clipboardData.setData('text', content);
            e.preventDefault(); //取消事件的默认动作
            document.oncopy = null;
          };
        })(content);
        document.execCommand('Copy');
      }
      this.$message.success('复制成功！');
    },

    resetPsw(row) {
          this.$confirm('确定要重置此账号的密码为"123456"吗?', '提示', {
              type: 'warning'
          })
              .then(() => {
                  const loading = this.$loading({lock: true});
                  resetPassword(row.id)
                      .then((msg) => {
                          loading.close();
                          this.$message.success(msg);
                      })
                      .catch((e) => {
                          loading.close();
                          this.$message.error(e.message);
                      });
              })
              .catch(() => {
              });
      },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return list({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      remove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 一键默认 */
    doDefault() {
      this.$confirm(
        '请谨慎使用该功能，一旦使用则会变成系统默认内容。确定一键默认？',
        '提示',
        {
          type: 'warning'
        }
      )
        .then(() => {
          const loading = this.$loading({ lock: true });
          doDefault()
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'status', value: row.status })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 0;
          this.$message.error(e.message);
        });
    }
  },
  mounted() {}
};
</script>

<style>
.drag {
  background: #000 !important;
  background-image: linear-gradient(#333, #999) !important;
}
</style>
